// pages/saveMusic/saveMusic.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:0,
    userid:0,
    musicList:[],
    infotitle:'',
    infolen:0,
    coverPath:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('transportPara', data=>{
        this.setData({
          nickName:data.nickName,
          userid:data.userid,
          musicList:data.music
        })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const app = getApp()
    this.setData({
      coverPath:app.globalData.curCover
    })
    console.log(app.globalData.curCover, this.data.coverPath)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  titleCheck: function(e) {
    let value = e.detail.value
    if(value.length===0){
      this.setData({
        infotitle:'必填',
        infolen:value.length
      })
      return
    }
    this.setData({
      infotitle:'',
      infolen:value.length
    })
  },

  descCheck: function(e) {
    let data = e.detail.value
    this.setData({
      infodesc:data.length
    })
  },

  update: function() {
    // console.log('update')
    wx.navigateTo({
      url: '/pages/clipImg/index',
    })
  },

  updateImg: function () {
    let that = this
    if(this.data.coverPath.length<3){
      this.update()
    }else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title:'是否替换之前的图片',
        confirmColor:'#f7de98',
        success:(res)=>{
          if(res.confirm){
            that.update()
          }else if(res.cancel){
            return
          }
        }
      })
    }
  },

  formSubmit: function(e){
    if(this.data.infolen===0) {
      wx.showToast({
        title: '请输入标题',
        icon:'error'
      })
      return
    }
    if(this.data.coverPath.length===0){
      wx.showToast({
        title: '请上传封面',
        icon:'error'
      })
      return
    }
    //console.log(e.detail)
    this.updateImg2Server(e.detail.value.title)
  },

  updateImg2Server: function(title) {
    let app = getApp()
    const formdata = {}
    let userid = wx.getStorageSync('userid')
    formdata.title = title
    formdata.userid = userid
    formdata.info = this.map2String(this.data.musicList)
    const that = this
    wx.uploadFile({
      filePath: that.data.coverPath,
      name: 'coverimg',
      url: 'https://thinker.skyseaee.com/saveMusic',
      formData:formdata,
      success(res){
        wx.showToast({
          title: '上传成功',
        }).then(reolve=>{
          app.globalData.updateStatus=1
          let userid = app.globalData.userid
          wx.request({
            url: 'https://thinker.skyseaee.com/getPortfolio',
            data:{
              userid:userid
            },
            success(res){
              let portfolio = res.data.portfolio
              let musicIDList = []
              // console.log(portfolio)
              for(let port of portfolio) {
                musicIDList.push(port.orderID)
              }
              app.globalData.musics = musicIDList
              wx.setStorage({
                data:musicIDList,
                key:'musics'
              })
            }
          })
          wx.navigateBack({
          })
        })
      }
    })
  },

  map2String(map){
    return JSON.stringify(map)
  }
})